package com.pda.common.pages.outbill;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.pda.common.R;
import com.pda.common.basemvp.activity.BaseActivity;
import com.pda.common.bean.BillBean;
import com.pda.common.pages.outscanf.OutScanfActivity;
import com.pda.common.scan.BroadcastConfig;
import com.pda.common.utils.PDAUtil;
import java.io.Serializable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OutBillActivity extends BaseActivity<OutBillModel, OutBillView, OutBillPresent> implements OutBillView {
    private String TAG = "OutBillActivity";
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvright)
    TextView tvright;
    @BindView(R.id.ivu)
    ImageView ivu;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.list)
    ListView list;
    OutBillAdapter outBillAdapter;

    protected BroadcastReceiver mScanfBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                presenter.showlist(intent.getStringExtra("scannerdata"));
            }
        }
    };

    @Override
    public OutBillModel createModel() {
        return new OutBillModelImpl(this);
    }

    @Override
    public OutBillView createView() {
        return this;
    }

    @Override
    public OutBillPresent createPresenter() {
        return new OutBillPresent();
    }

    @Override
    public void showlist(final BillBean data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                intentToActivityWithSParameter(OutBillActivity.this, OutScanfActivity.class, PDAUtil.keyBillDeatial, (Serializable)data);
                OutBillActivity.this.finish();
            }
        });
    }

    @Override
    public void tip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
            }
        });
    }

    @Override
    protected void initdata() {
        tvTitle.setText("出库订单");
        list.setVisibility(View.GONE);
        View headView = getLayoutInflater().inflate(R.layout.list_outbillhead, null);
        list.addHeaderView(headView);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0)
                {
                    return;
                }
                int index=position-1;
                outBillAdapter.setSelectedPosition(index);
                outBillAdapter.notifyDataSetInvalidated();
            }
        });
        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        registerReceiver(mScanfBroadcastReceiver, intentFilter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_outbill);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @OnClick({R.id.iv_title_left,R.id.ivu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.ivu:
                if(loginId.getText().toString()!=null && loginId.getText().toString().length()>0)
                {
                    presenter.showlist(loginId.getText().toString());
                }
                else
                {
                    showMsg("查询请输入单号");
                }
                break;
        }
    }
}
